package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.SysUser;
import com.example.studentstatusmanagement.pojo.SysUserExample;
import com.example.studentstatusmanagement.pojo.doo.ReqHygiene;
import com.example.studentstatusmanagement.pojo.doo.ReqMajor;
import com.example.studentstatusmanagement.pojo.doo.ReqMsg;
import com.example.studentstatusmanagement.pojo.doo.ReqSMsg;
import com.example.studentstatusmanagement.pojo.dto.SHygiene;
import com.example.studentstatusmanagement.pojo.dto.SMajor;
import com.example.studentstatusmanagement.pojo.dto.SMsg;
import com.example.studentstatusmanagement.service.HygieneService;
import com.example.studentstatusmanagement.service.MsgService;
import com.example.studentstatusmanagement.service.UserService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/student")
public class StudentController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private HygieneService hygieneService;

    @Autowired
    private MsgService msgService;

    /**
     * 软删除为修改所以固定为修改
     * 普通修改机制
     * @param sysUser
     * @return
     */
    @RequestMapping("del")
    public ResultResponse del(SysUser sysUser) {
        if (!StringUtils.isEmpty(sysUser.getId())) {
            sysUser.setUserIsdel(SystemVariable.IS_DEL);
            if (userService.updateByPrimaryKeyUserSelective(sysUser) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("use")
    public ResultResponse use(SysUser sysUser) {
        if (!StringUtils.isEmpty(sysUser.getId())) {
            sysUser.setUserIsdel(SystemVariable.IS_NOT_DEL);
            if (userService.updateByPrimaryKeyUserSelective(sysUser) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("update")
    public ResultResponse update(SysUser sysUser) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria c = sysUserExample.createCriteria();
        c.andIdNotEqualTo(sysUser.getId());
        c.andUserNumberEqualTo(sysUser.getUserNumber());

        if (userService.selectByExample(sysUserExample).size()>0){
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        }
        if (!StringUtils.isEmpty(sysUser.getId())) {
            if (userService.updateByPrimaryKeyUserSelective(sysUser) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("show")
    public ResultResponse show(Integer id){
        if (StringUtils.isEmpty(id))
            return  ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
        return ResultResponse.Build(ResultTypeEnum.success,userService.selectStudentInfoUpdateById(id));
    }

    @RequestMapping("showmyhygiene")
    public Map<String, Object> showmyhygiene(ReqHygiene hygiene ) {

        Map<String, Object> so = new HashMap<String, Object>();

        hygiene.setDid(getUser().getUserDormid());
        List<SHygiene> ls = hygieneService.selectStudentHygienes(hygiene);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);
        return so;
    }

    @RequestMapping("add")
    public ResultResponse addstudent(SysUser sysUser){

        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria c = sysUserExample.createCriteria();
        c.andUserNumberEqualTo(sysUser.getUserNumber());
        if (userService.selectByExample(sysUserExample).size()>0){
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.NUMBER_IS_HAVE);
        }
        sysUser.setUserPassword("123456");
        sysUser.setUserRoleid(2);
        if (userService.insertSelective(sysUser) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("showmsg")
    public Map<String, Object> showMsg(ReqSMsg reqMsg) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SMsg> ls = msgService.selectMsgsByStu(reqMsg);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }
}
